package goodenglish.domain.service.impl;

import goodenglish.application.SubtImportService;
import goodenglish.application.impl.SubtImportServiceImpl;
import goodenglish.domain.model.lexicon.NewWord;
import goodenglish.domain.model.subt.Subt;
import goodenglish.domain.model.subt.SubtFactory;
import goodenglish.domain.model.subt.SubtSentence;
import goodenglish.domain.service.ServiceException;
import goodenglish.domain.service.SubtBackService;
import goodenglish.domain.service.SubtInType;
import goodenglish.infrastructure.Config;
import goodenglish.infrastructure.DateUtil;
import goodenglish.infrastructure.FileUtil;
import goodenglish.interfaces.BeanFactory;

import java.io.File;
import java.util.Collections;

/**
 * 
 * @author qiuriyuchen E-mail:panpan_001@126.com
 */
public class SubtBackServiceFile implements SubtBackService {
	public static final String SEPARATE = "-/-/-------------------/-/";

	@Override
	public void backSubt(Subt subt) {
		File backFile = new File(Config.BACK_PATH + "/" + subt.getName() + DateUtil.getCurrTimeMiniteStr() + ".txt");
		StringBuffer buffer = getSubtSentencesStringBuffer(subt);
		buffer.append(Config.LINE_SPLITER);
		buffer.append(SEPARATE);
		buffer.append(Config.LINE_SPLITER);
		buffer.append(getSubtWordsStringBuffer(subt));
		FileUtil.writeStrToTxtFileReplaceIfExsit(backFile, buffer.toString());

	}

	@Override
	public Subt reBackSubt(File file) throws ServiceException {
		Subt subt = subtFactory.createSubt(file, SubtInType.REBACK);
		subtService.saveSubtAndLexicon(subt);

		return subt;
	}

	public StringBuffer getSubtWordsStringBuffer(Subt subt) {

		StringBuffer exportBuffer = new StringBuffer();
		Collections.sort(subt.getLexicon().getSpells());
		for (NewWord word : subt.getLexicon().getNewWords()) {

			if (null == word.getSpell() || word.getSpell().trim().isEmpty()) {
				continue;
			}
			exportBuffer.append(word.getSpell());
			if (word.getExplain() != null) {
				exportBuffer.append(Config.WORD_SPLITER);
				exportBuffer.append(word.getExplain());
			}
			exportBuffer.append(Config.LINE_SPLITER);

		}
		return exportBuffer;

	}

	public StringBuffer getSubtSentencesStringBuffer(Subt subt) {

		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < subt.getSubtSentences().size(); i++) {
			SubtSentence subtSentence = subt.getSubtSentences().get(i);
			buffer.append(i + 1);
			buffer.append(Config.LINE_SPLITER);
			// 字幕时间字幕串
			buffer.append(subtSentence.getStimeStr());
			buffer.append(Config.TIME_SPLITER);
			buffer.append(subtSentence.getEtimeStr());
			buffer.append(Config.LINE_SPLITER);
			// 字幕时间毫秒
			buffer.append(subtSentence.getStime());
			buffer.append(Config.TIME_SPLITER);

			buffer.append(subtSentence.getEtime());
			buffer.append(Config.LINE_SPLITER);
			// 英文
			buffer.append(subtSentence.getEnStr());
			buffer.append(Config.LINE_SPLITER);
			// 中文
			buffer.append(subtSentence.getChStr());
			buffer.append(Config.LINE_SPLITER);
			buffer.append(Config.LINE_SPLITER);
		}
		return buffer;

	}

	private SubtImportService subtService = BeanFactory.getSingleInstance(SubtImportServiceImpl.class);
	private SubtFactory subtFactory = BeanFactory.getSingleInstance(SubtFactory.class);

}
